package com.gt.utils;

import org.apache.log4j.Logger;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

/**
 * 发送邮件工具类，使用qq邮箱发送
 */
public class EmailUtil2 {
    //qq邮箱
    private static Logger logger = Logger.getLogger(EmailUtil2.class);
    private final static String SEND_PORT = "587";
    private static final String EMAIL_HOST = "smtp.qq.com";
    private static final String EMAIL_FROM = "729135975@qq.com";
    private static final String EMAIL_PASSWORD = "vgacetlqohdbbfci";
    //公司邮箱
    private final static String SEND_PORT2 = "2626";
    private static final String EMAIL_HOST2 = "mail.gtexpress.cn";
    private static final String EMAIL_FROM2 = "gtdebug@gtexpress.cn";
    private static final String EMAIL_PASSWORD2 = "gtdebug";


    /**
     *
     * @param type      1:使用公司邮箱发件  2：使用qq邮箱发件
     * @param nick      昵称
     * @param mailTo    收件人
     * @param subject   主题
     * @param content   内容 text或者html
     * @return          true或者false
     */
    public static boolean sendEmail(int type, String nick, String mailTo, String subject, String content){
        try {
            Properties props = new Properties();
            if(type == 1){
                props.put("mail.smtp.host", EMAIL_HOST2);
                props.put("mail.smtp.port", SEND_PORT2);
                props.put("mail.user", EMAIL_FROM2);
                props.put("mail.password", EMAIL_PASSWORD2);
                logger.info("使用公司邮箱发件");
            }else{
                props.put("mail.smtp.host", EMAIL_HOST);
                props.put("mail.smtp.port", SEND_PORT);
                props.put("mail.user", EMAIL_FROM);
                props.put("mail.password", EMAIL_PASSWORD);
                logger.info("使用qq邮箱发件");
            }
            //鉴权验证
            props.put("mail.smtp.auth", "true");
            Session session = Session.getDefaultInstance(props, new Authenticator(){
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(props.getProperty("mail.user"), props.getProperty("mail.password"));
                }});
            MimeMessage msg = new MimeMessage(session);
            InternetAddress fromAddress = null;
            //设置自定义发件人昵称
            if(null != nick && !"".equals(nick)){
                try {
                    nick= MimeUtility.encodeText(nick);
                    fromAddress = new InternetAddress(nick+" <" + props.getProperty("mail.user") + ">");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            } else {
                fromAddress = new InternetAddress(props.getProperty("mail.user"));
            }
            // 设置发件人地址信息
            msg.setFrom(fromAddress);
            // 设置收件人地址信息
            String[] to = mailTo.split(",");
            for(int i = 0; i < to.length;i++) {
                InternetAddress toAddress = new InternetAddress(to[i]);
                msg.addRecipient(Message.RecipientType.TO,toAddress);
            }
            //设置邮件主题
            msg.setSubject(subject);
            // 邮件内容
            msg.setContent(content, "text/html;charset=UTF-8");
            //发送时间
            msg.setSentDate(new Date());
            Transport.send(msg);
            return true;
        } catch (AddressException e) {
            logger.error("邮件发送异常原因", e);
            e.printStackTrace();
            return false;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) throws MessagingException {
        StringBuilder sb = new StringBuilder();
        sb.append("<html>");
        sb.append("<body>");
        sb.append("<center>");
        sb.append("<div style=\"margin-top:0px;background-color: #F09B22;width: 70%; height: 40%;padding: 10% 20%;\">");
        sb.append("<p><img src=\"http://39.106.249.147:9999/css/assets/img/logo_red.png\" style=\"width: 250px; height: 80px;\" /></p>");
        sb.append("<p style=\"color: white\">尊敬的");
        sb.append("项目有错误信息产生,请及时查看。");
        sb.append("</p>");
        sb.append("<p style=\"background-color:#3498db;border-radius:10px;width: 150px; height: 50px; line-height: 50px; border: none;color:white;margin-top: 50px;\">");
        sb.append("<a style=\"text-decoration:none;color:white;\">查看详情</a>");
        sb.append("</p>");
        sb.append("</div>");
        sb.append("</center>");
        sb.append("</body>");
        sb.append("</html>");
        boolean flag = EmailUtil2.sendEmail(2,"gtdebug", "wanghuiyong@gtexpress.cn,729135975@qq.com", "主题", sb.toString());
        System.out.println(flag);
    }

}
